//
//  EFengMIneContactController.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/1/21.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "EFengMIneContactController.h"
#import "GroupMenberCell.h"
#import "GroupMemberDetailVC.h"
#import "UIImageView+WebCache.h"
#import "CContactModel.h"
#import "CXmppRequestMgr.h"
#import "CContactMgr.h"
#import "NSArray+PinYin.h"
#import "CAccountModel.h"
#import "CFolderMgr.h"
#import "CContactModel.h"
#import "MyInformationVC.h"
#import "CRecentlyContactMgr.h"
#import "CRecentlyContactModel.h"
#import "CGroupModel.h"
#import "CGroupMgr.h"
#import "CHChatRoomBase.h"
#import "IMSearchDisplayController.h"
#import "CDepartmentMgr.h"
#import "CDepartmentModel.h"
#import "ChildHoodService.h"
#import "JKAlertDialog.h"
#import "WZHeader.h"
#import "WZHeaderView.h"


#define KSECTIONHEIGHT 35.0f
#define KNORMALCELLHEIGHT 60.0f

@interface EFengMIneContactController ()<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate, WZHeaderViewDelegate>

{
    UITableView *_tableView;
    UISearchBar *_searchBar;
    IMSearchDisplayController *_searchDisplayController;
    NSMutableArray *_searchAllFirstLetter;
    NSMutableArray *_searchSectionListArray;
    CContactMgr *_conMgr;

}

//@property (nonatomic, strong) NSMutableArray *contactList;

@property (nonatomic, strong) NSMutableArray *kefuList;
@property (nonatomic, strong) NSMutableArray *changyongList;
@property (nonatomic, strong) NSMutableArray *waibuList;
@property (nonatomic, strong) CContactModel *curContact;
@property (nonatomic, strong) NSMutableArray *sectionArray;

@property (nonatomic, strong) WZHeader *kefuHeader;
@property (nonatomic, strong) WZHeader *waibuHeader;
@property (nonatomic, strong) WZHeader *changyongHeader;

@end

@implementation EFengMIneContactController

//- (NSMutableArray *)contactList
//{
//    if (_contactList == nil) {
//        _contactList = [NSMutableArray array];
//    }
//    return _contactList;
//}

- (NSMutableArray *)kefuList
{
    if (_kefuList == nil) {
        _kefuList = [NSMutableArray array];
    }
    return _kefuList;
}

- (NSMutableArray *)waibuList
{
    if (_waibuList == nil) {
        _waibuList = [NSMutableArray array];
    }
    return _waibuList;
}

- (NSMutableArray *)changyongList
{
    if (_changyongList == nil) {
        _changyongList = [NSMutableArray array];
    }
    return _changyongList;
}

- (WZHeader *)kefuHeader
{
    if (_kefuHeader == nil) {
        _kefuHeader = [[WZHeader alloc] init];
    }
    return _kefuHeader;
}

- (WZHeader *)waibuHeader
{
    if (_waibuHeader == nil) {
        _waibuHeader = [[WZHeader alloc] init];
    }
    return _waibuHeader;
}
- (WZHeader *)changyongHeader
{
    if (_changyongHeader == nil) {
        _changyongHeader = [[WZHeader alloc] init];
    }
    return _changyongHeader;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPDIDGERECENTLYLIST object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPDIDGETKEFULIST object:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (IsIOS7) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addKeFuContact) name:K_XMPPDIDGETKEFULIST object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addOftenContact) name:K_XMPPDIDGERECENTLYLIST object:nil];
    
    // Do any additional setup after loading the view.
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationItem setNewTitleCenter:@"我的联系人"];
    self.view.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    _conMgr = [[CContactMgr alloc] init];
    self.curContact = [_conMgr loadCurContact];
    _tableView = [[UITableView alloc] init];
    //    _tableView.frame = self.view.frame;
    _tableView.frame = CGRectMake(0, 44,HARDWARE_SCREEN_WIDTH,HARDWARE_SCREEN_HEIGHT - 64 - 44);
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.sectionIndexBackgroundColor = ClearColor;
    _tableView.sectionIndexTrackingBackgroundColor = ClearColor;
    
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    _searchBar.backgroundImage = [UIImage imageNamed:@"BG-search"];
    //test 联系人搜索
    _searchBar.placeholder = @"搜索";
    _searchBar.delegate = self;
    // 添加 searchbar 到 headerview
    [self.view addSubview:_searchBar];
    
    _searchDisplayController = [[IMSearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
    
    // searchResultsDataSource 就是 UITableViewDataSource
    _searchDisplayController.searchResultsDataSource = self;
    // searchResultsDelegate 就是 UITableViewDelegate
    _searchDisplayController.searchResultsDelegate = self;
    _searchDisplayController.delegate = self;
    _searchDisplayController.searchResultsTableView.sectionIndexColor = RGBCOLOR(0x64, 0x64, 0x64);
    _searchDisplayController.searchResultsTableView.sectionIndexBackgroundColor = ClearColor;
    _searchDisplayController.searchResultsTableView.sectionIndexTrackingBackgroundColor = ClearColor;
    
    self.view.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);

}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (_searchDisplayController.active) {
        [_searchDisplayController setActive:NO];
    }
    
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    if (IsIOS7) {
        [self.navigationController.navigationBar setBarTintColor:RGBCOLOR(44, 170, 253)];
    } else {
        [self.navigationController.navigationBar setTintColor:RGBCOLOR(44, 170, 253)];
    }
    
    _tableView.frame = CGRectMake(0, 44,HARDWARE_SCREEN_WIDTH,HARDWARE_SCREEN_HEIGHT - 64 - 44);
    
    [self addOftenContact];
    [self addKeFuContact];
    [self addWaiBuContact];
    
    if (self.kefuList.count > 0) {
        self.kefuHeader.headerText = [NSString stringWithFormat:@"%@(%zd)",@"万朝客服",self.kefuList.count];
        self.kefuHeader.rowArray = self.kefuList;
        self.kefuHeader.isEdit = NO;
    }
    if (self.waibuList.count > 0) {
        self.waibuHeader.headerText = [NSString stringWithFormat:@"%@(%zd)",@"外部联系人",self.waibuList.count];
        self.waibuHeader.rowArray = self.waibuList;
        self.waibuHeader.isEdit = NO;
    }
    if (self.changyongList.count > 0) {
        self.changyongHeader.headerText = [NSString stringWithFormat:@"%@(%zd)",@"常用联系人",self.changyongList.count];
        self.changyongHeader.rowArray = self.changyongList;
        self.changyongHeader.isEdit = YES;
    }

}

- (void)addKeFuContact
{
   
    [self.kefuList removeAllObjects];
    NSArray *array = [_conMgr loadKeFuList];
    if (self.isFromFoward) {
        [array enumerateObjectsUsingBlock:^(CContactModel *obj, NSUInteger idx, BOOL *stop) {
            if (![obj.UserID isEqualToString:self.curContact.UserID]) {
                [self.kefuList addObject:obj];
            }
        }];
 
    } else {
        [self.kefuList addObjectsFromArray:array];
    }
    [_tableView reloadData];
}

- (void)addWaiBuContact
{
    
    [self.waibuList removeAllObjects];
    NSArray *array = [_conMgr loadWaiBuList];
    if (self.isFromFoward) {
        [array enumerateObjectsUsingBlock:^(CContactModel *obj, NSUInteger idx, BOOL *stop) {
            if (![obj.UserID isEqualToString:self.curContact.UserID]) {
                [self.waibuList addObject:obj];
            }
        }];
        
    } else {
        [self.waibuList addObjectsFromArray:array];
    }
    [_tableView reloadData];
}



- (void)addOftenContact
{
    
    [self.changyongList removeAllObjects];
    NSArray *array = [[[CRecentlyContactMgr alloc] init] loadAllRecentlyContacts];
    
    for (CRecentlyContactModel *model in array) {
        CContactModel *contact = [_conMgr loadNotDelContactWithUserId:model.userId];
        if (contact) {
            contact.yuLiuZiduan3 = @"";
            [self.changyongList addObject:contact];
        }
    }
   
    [_tableView reloadData];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if(tableView == _tableView)
    {
        NSInteger rows = 0;
        WZHeader *header = [self header:section];
        if (header.isOpen) {
           rows = header.rowArray.count;
        } else {
            rows = 0;
        }
        
        return rows;
    }
    else
    {
        NSArray *array = _searchSectionListArray[section];
        return array.count;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView              // Default is 1 if not implemented
{
    if(tableView == _tableView)
    {
        NSInteger sections = 0;
        if (self.kefuList.count > 0) {
            sections ++;
        }
        if (self.waibuList.count > 0) {
            sections ++;
        }
        if (self.changyongList.count > 0) {
            sections ++;
        }

        
        return sections;
    }
    else
    {
        return _searchSectionListArray.count;
    }
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if(tableView == _tableView)
    {
        
        WZHeader *header = [self header:indexPath.section];
        CContactModel *contact = header.rowArray[indexPath.row];

        static NSString *NormalCellIdentifier = @"GroupMenberCell";
        GroupMenberCell *groupMemberCell = [tableView dequeueReusableCellWithIdentifier:NormalCellIdentifier];
        if (groupMemberCell == nil) {
            groupMemberCell = [[[NSBundle mainBundle] loadNibNamed:@"GroupMenberCell" owner:self options:nil] objectAtIndex:0];
        }
        
        if ([contact.yuLiuZiduan3 isEqualToString:@"isKefu"]) {
            [groupMemberCell.icon setImage:[UIImage imageNamed:@"head-kefu"]];
        } else {
            [groupMemberCell.icon sd_setImageWithURL:[NSURL URLWithString:[[CXmppRequestMgr shareInstance] DownHeadFileWithContact:contact.UserID]] placeholderImage:[UIImage imageNamed:@"80-80"]];
        }
        //        [groupMemberCell.titleLabel setText:@"测试名字"];
        [groupMemberCell.titleLabel setText:contact.UserName];
        groupMemberCell.Arrow.hidden = NO;
        NSInteger rows = [tableView numberOfRowsInSection:indexPath.section];
        if(indexPath.row == rows - 1)
        {
            [groupMemberCell.seperatorLine setHidden:YES];
        } else {
            groupMemberCell.seperatorLine.hidden = NO;
        }
        return groupMemberCell;
        
    } else {
        NSArray *arry = _searchSectionListArray[indexPath.section];
        CContactModel *contact = arry[indexPath.row];

        static NSString *GroupCellIdentifier = @"GroupMenberCell";
        GroupMenberCell *groupMemberCell = [tableView dequeueReusableCellWithIdentifier:GroupCellIdentifier];
        if (groupMemberCell == nil) {
            groupMemberCell = [[[NSBundle mainBundle] loadNibNamed:@"GroupMenberCell" owner:self options:nil] objectAtIndex:0];
        }
        groupMemberCell.Arrow.hidden = YES;
        [groupMemberCell.icon sd_setImageWithURL:[NSURL URLWithString:[[CXmppRequestMgr shareInstance] DownHeadFileWithContact:contact.UserID]] placeholderImage:[UIImage imageNamed:@"80-80"]];
        //        [groupMemberCell.titleLabel setText:@"测试名字"];
        [groupMemberCell.titleLabel setText:contact.UserName];
        NSInteger rows = [tableView numberOfRowsInSection:indexPath.section];
        
        if(indexPath.row == rows - 1)
        {
            [groupMemberCell.seperatorLine setHidden:YES];
        } else {
            groupMemberCell.seperatorLine.hidden = NO;
        }
        return groupMemberCell;
        
    }
}

-(WZHeader *)header:(NSInteger)section
{
    WZHeader *header = nil;
    switch (section) {
        case 0:
            {
            if (self.kefuList.count > 0) {
                header = self.kefuHeader;
            } else if (self.waibuList.count > 0) {
                header = self.waibuHeader;
            } else if (self.changyongList.count > 0){
                header = self.changyongHeader;
            }
        }
            break;
        case 1:
        {
            if (self.kefuList.count <= 0) {
                header = self.changyongHeader;
            } else {
                if (self.waibuList.count > 0) {
                    header = self.waibuHeader;
                } else {
                    header = self.changyongHeader;
                }
            }
        }
            break;
        case 2:
            {
                header = self.changyongHeader;
            }
            break;
        default:
            break;
    }
    return header;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    if (_tableView == tableView) {
        
        
        return KSECTIONHEIGHT;
    } else {
        return KSECTIONHEIGHT;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    CGFloat height = 0.01f;
    switch (section) {
        case 1:
            height = 0.01f;
            break;
        default:
            break;
    }
    return height;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section   // custom view for header. will be adjusted to default or specified header height
{
    UIView *headerView = nil;
   
    if (_tableView == tableView) {
        
        WZHeaderView *header = [WZHeaderView WZHeaderViewWithTableView:tableView];
        header.header = [self header:section];
        header.WZHeaderViewDelegate = self;
        headerView = header;
//        headerView = [[UIView alloc] init];
//        headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, KSECTIONHEIGHT);
//        headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
//        UIView *beginLine = [[UIView alloc] init];
//        beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
//        [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
//        [headerView addSubview:beginLine];
//        
//        UIView *endLine = [[UIView alloc] init];
//        endLine.frame = CGRectMake(0, KSECTIONHEIGHT-0.5, tableView.frame.size.width, 0.5);
//        [endLine setBackgroundColor:KSEPERATORLINECOLOR];
//        [headerView addSubview:endLine];
//        
//        UILabel *label = [[UILabel alloc] init];
//        
//        [label setText:[NSString stringWithFormat:@"%@(%zd)",[self header:section],rows]];
//        [label setTextColor:RGBCOLOR(0x92, 0x92, 0x92)];
//        label.frame = CGRectMake(15, 1, 200, KSECTIONHEIGHT-1);
//        [headerView addSubview:label];
            
    } else {
        
        headerView = [[UIView alloc] init];
        headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, KSECTIONHEIGHT);
        headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
        UIView *beginLine = [[UIView alloc] init];
        beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
        [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
        [headerView addSubview:beginLine];
        
        UIView *endLine = [[UIView alloc] init];
        endLine.frame = CGRectMake(0, KSECTIONHEIGHT-0.5, tableView.frame.size.width, 0.5);
        [endLine setBackgroundColor:KSEPERATORLINECOLOR];
        [headerView addSubview:endLine];
        
        UILabel *label = [[UILabel alloc] init];
        label.text = _searchAllFirstLetter[section];
        [label setTextColor:RGBCOLOR(0x92, 0x92, 0x92)];
        label.frame = CGRectMake(15, 1, 200, KSECTIONHEIGHT-1);
        [headerView addSubview:label];
        
        
    }
    
    return headerView;
}


- (void)WZHeaderViewDidClick:(WZHeader *)header
{
    header.isOpen = !header.isOpen;
    [_tableView reloadData];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section   // custom view for footer. will be adjusted to default or specified footer height
{
    UIView *headerView = nil;
    //    if(section != 1)
    //    {
    //        return headerView;
    //    }
    //    headerView = [[UIView alloc] init];
    //    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, 2);
    //    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    //    UIView *beginLine = [[UIView alloc] init];
    //    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
    //    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
    //    [headerView addSubview:beginLine];
    return headerView;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tableView == tableView) {
        CGFloat height = 60.0f;
        
        if(indexPath.row == 0 && indexPath.section == 0)
        {
            height = 60.0f;
        }
        return height;
        
    } else {
        return 60.0f;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.view endEditing:YES];
    if (self.isFromFoward) {
        if (tableView == _tableView) {
        WZHeader *header = [self header:indexPath.section];
        CContactModel *contact2 = header.rowArray[indexPath.row];
        
        CContactModel *currentUser = [_conMgr loadContactWithUserId:[[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount].userId];
        JKAlertDialog *alert = [[JKAlertDialog alloc]initWithTitle:@"确定发送给:" message:contact2.UserName];
        [alert addButton:Button_OK withTitle:@"确定" handler:^(JKAlertDialogItem *item) {
            
            [self fowardWithMember:contact2 withCurUser:currentUser];
        }];
        
        [alert addButton:Button_CANCEL withTitle:@"取消" handler:^(JKAlertDialogItem *item) {
            
        }];
        [alert show];
        }
        else
        {
             CContactModel *currentUser = [_conMgr loadContactWithUserId:[[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount].userId];
            NSArray *allrow = _searchSectionListArray[indexPath.section];
            CContactModel *contact2 = allrow[indexPath.row];
            JKAlertDialog *alert = [[JKAlertDialog alloc]initWithTitle:@"确定发送给:" message:contact2.UserName];
            [alert addButton:Button_OK withTitle:@"确定" handler:^(JKAlertDialogItem *item) {
                
                [self fowardWithMember:contact2 withCurUser:currentUser];
            }];
            
            [alert addButton:Button_CANCEL withTitle:@"取消" handler:^(JKAlertDialogItem *item) {
                
            }];
            [alert show];
        }
        
    } else {
        if (tableView == _tableView) {
            WZHeader *header = [self header:indexPath.section];
            CContactModel *contact = header.rowArray[indexPath.row];

            CDepartmentModel *dep = [[[CDepartmentMgr alloc] init] loadDepartment:contact.DEPTID];
            [self showMembersDetailVCWithMember:contact deptName:dep.DepartName];
            
        } else {
            NSArray *array1 = _searchSectionListArray[indexPath.row];
            CContactModel *model = array1[indexPath.row];
            CDepartmentModel *dep = [[[CDepartmentMgr alloc] init] loadDepartment:model.DEPTID];
            [self showMembersDetailVCWithMember:model deptName:dep.DepartName];
            
        }
   
    }
    
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if (_tableView == tableView) {
        return nil;
    } else{
        return _searchAllFirstLetter;
    }
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    NSInteger count = 0;
    if(tableView == _tableView)
    {
        count = index;
    } else {
        count = index;
    }
    return count;
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tableView == tableView) {
        WZHeader *header = [self header:indexPath.section];
        if (self.isFromFoward) {
            return UITableViewCellEditingStyleNone;
        } else {
            if (header.isEdit) {
                return UITableViewCellEditingStyleDelete;
            } else {
                return UITableViewCellEditingStyleNone;
            }
 
        }
        } else {
        return UITableViewCellEditingStyleNone;
    }
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tableView == tableView) {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            
        
            CContactModel *contact = self.changyongList[indexPath.row];

            CRecentlyContactMgr *reMgr = [[CRecentlyContactMgr alloc] init];
            [reMgr delRecentlyContact:contact.UserID];
            [[CXmppRequestMgr shareInstance] delOftenContactWithUserId:contact.UserID];
            [self addOftenContact];
            [_tableView reloadData];
        }
        else if (editingStyle == UITableViewCellEditingStyleInsert) {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
        
    } else {
        
        
    }
    
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_tableView == tableView) {
        WZHeader *header = [self header:indexPath.section];
        if (self.isFromFoward) {
            return NO;
        } else {
            return header.isEdit;
        }
    } else {
        return NO;
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)
{
    return @"删除";
}



//MyInformationVC
-(void)showMembersDetailVCWithMember:(CContactModel *)model deptName:(NSString *)deptName
{
    
    //    CHSNavigationController *groupMemberDetailNav = [[CHSNavigationController alloc] initWithRootViewController:groupMemberDetail];
    if ([[[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount].userId isEqualToString:model.UserID]) {
        
        MyInformationVC *myInfo = [[MyInformationVC alloc] init];
        //    CHSNavigationController *myInfoNav = [[CHSNavigationController alloc] initWithRootViewController:myInfo];
        [self.navigationController pushViewController:myInfo animated:YES];
        //    [self.leveyTabBarController setHidesBottomBarWhenPushed:YES];
        
    }else {
        //    [self.leveyTabBarController presentViewController:departDetailNav animated:YES completion:nil];
        GroupMemberDetailVC *groupMemberDetail = [[GroupMemberDetailVC alloc] init];
        groupMemberDetail.member = model;
        groupMemberDetail.deptName = deptName;
        
        [self.navigationController pushViewController:groupMemberDetail animated:YES];
        
    }
    
}

//-(void)showMembersDetailVC
//{
//    GroupMemberDetailVC *groupMemberDetail = [[GroupMemberDetailVC alloc] init];
//    CHSNavigationController *groupMemberDetailNav = [[CHSNavigationController alloc] initWithRootViewController:groupMemberDetail];
//    //    [self.leveyTabBarController presentViewController:departDetailNav animated:YES completion:nil];
//    [self.navigationController pushViewController:groupMemberDetail animated:YES];
//    [self.leveyTabBarController hidesTabBar:YES animated:NO];
//}


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [_searchSectionListArray removeAllObjects];
    [_searchAllFirstLetter removeAllObjects];
    NSArray *contactArray = [[[CContactMgr alloc] init] searchContactWithSeachStr:searchText];
    CContactModel *currentUser = [_conMgr loadContactWithUserId:[[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount].userId];
    //bug 8849 fixed by lixu
    //bug 9516 fixed by lixu
    NSMutableSet *searchSet = [NSMutableSet set];
//    NSMutableArray *searchArray = [NSMutableArray array];
   [contactArray enumerateObjectsUsingBlock:^(CContactModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
       for (CContactModel *model in self.kefuList) {
           if ([obj.UserID isEqualToString:model.UserID]&&![obj.UserID isEqualToString:currentUser.UserID]) {
               [searchSet addObject:obj];
           }
       }
       for (CContactModel *model in self.changyongList) {
           if ([obj.UserID isEqualToString:model.UserID]&&![obj.UserID isEqualToString:currentUser.UserID]) {
               [searchSet addObject:obj];
           }
       }
       for (CContactModel *model in self.waibuList) {
           if ([obj.UserID isEqualToString:model.UserID]&&![obj.UserID isEqualToString:currentUser.UserID]) {
               [searchSet addObject:obj];
           }
       }
   }];
    NSMutableArray *searchArray = [[searchSet allObjects] mutableCopy];
    _searchAllFirstLetter = [NSArray IndexArray:searchArray];
    _searchSectionListArray = [NSArray LetterSortArray:searchArray];

}
//bug 8510 fixed by lixu
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView NS_DEPRECATED_IOS(3_0,8_0){
    [tableView setContentInset:UIEdgeInsetsZero];
    [tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
    tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString

{ for(UIView *subview in controller.searchResultsTableView.subviews) {
    
    if([subview isKindOfClass:[UILabel class]]) {
        
        [(UILabel*)subview setText:@"未找到匹配的信息"];
        
    }
    
}
    return YES;
}


- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar                     // called when text starts editing
{
    searchBar.showsCancelButton = YES;
    for(id cc in [_searchDisplayController.searchBar subviews])
    {
        if([cc isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)cc;
            [btn setTitle:@"取消"  forState:UIControlStateNormal];
        }
    }
    
}



#pragma mark-- UISearchDisplayDelegate
- (void) searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller NS_DEPRECATED_IOS(3_0,8_0)
{
    UIView *topView = controller.searchBar.subviews[0];
    
    for (UIView *subView in topView.subviews) {
        
        if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
            
            UIButton * cancelButton = (UIButton*)subView;
            
            [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
            
        }
        
    }
}

//转发
-(void)fowardWithMember:(CContactModel *)member withCurUser:(CContactModel *)currentUser
{

    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
    
    CHChatRoomBase *chat = [[CHChatRoomBase alloc] init];
    chat.member = member;
    chat.group = nil;
    
    UINavigationController *rotNav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *vc = rotNav.childViewControllers[1];
    [rotNav popToViewController:vc animated:NO];
    
    [rotNav pushViewController:chat animated:YES];
    if ([ChildHoodService defaultService].forwardText !=nil) {
        [[CXmppRequestMgr shareInstance] sendMessageWithMessage:[ChildHoodService defaultService].forwardText messageId:nil curUser:currentUser member:member group:nil];
    }
    if ([ChildHoodService defaultService].fileModel == nil && [ChildHoodService defaultService].imgLocalFilePath.length > 0) {
        UIImage *image = [UIImage imageWithContentsOfFile:[ChildHoodService defaultService].imgLocalFilePath];
        [[CXmppRequestMgr shareInstance] sendImageMessage:image messageId:nil index:0 curUser:currentUser member:member group:nil fileName:nil];
        //            [self cancel];
    } else {
        [[CXmppRequestMgr shareInstance] sendFile:[ChildHoodService defaultService].fileModel messageId:nil curUser:currentUser member:member group:nil];
        //            [self cancel];
    }

}

@end
